package cn.iocoder.yudao.module.ai.controller.admin.componentrepository;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.componentrepository.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.componentrepository.ComponentRepositoryDO;
import cn.iocoder.yudao.module.ai.service.componentrepository.ComponentRepositoryService;

@Tag(name = "管理后台 - 组件库")
@RestController
@RequestMapping("/ai/component")
@Validated
public class ComponentRepositoryController {

    @Resource
    private ComponentRepositoryService componentRepositoryService;

    @PostMapping("/create")
    @Operation(summary = "创建组件库")
    @PreAuthorize("@ss.hasPermission('ai:component-repository:create')")
    public CommonResult<Long> createComponentRepository(@Valid @RequestBody ComponentRepositorySaveReqVO createReqVO) {
        createReqVO.setSuplierId(SecurityFrameworkUtils.getLoginUserDeptId());
        return success(componentRepositoryService.createComponentRepository(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组件库")
    @PreAuthorize("@ss.hasPermission('ai:component-repository:update')")
    public CommonResult<Boolean> updateComponentRepository(@Valid @RequestBody ComponentRepositorySaveReqVO updateReqVO) {
        componentRepositoryService.updateComponentRepository(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组件库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:component-repository:delete')")
    public CommonResult<Boolean> deleteComponentRepository(@RequestParam("id") Long id) {
        componentRepositoryService.deleteComponentRepository(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组件库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:component-repository:query')")
    public CommonResult<ComponentRepositoryRespVO> getComponentRepository(@RequestParam("id") Long id) {
        ComponentRepositoryDO componentRepository = componentRepositoryService.getComponentRepository(id);
        return success(BeanUtils.toBean(componentRepository, ComponentRepositoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组件库分页")
    @PreAuthorize("@ss.hasPermission('ai:component-repository:query')")
    public CommonResult<PageResult<ComponentRepositoryRespVO>> getComponentRepositoryPage(@Valid ComponentRepositoryPageReqVO pageReqVO) {
        PageResult<ComponentRepositoryDO> pageResult = componentRepositoryService.getComponentRepositoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ComponentRepositoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组件库 Excel")
    @PreAuthorize("@ss.hasPermission('ai:component-repository:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportComponentRepositoryExcel(@Valid ComponentRepositoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ComponentRepositoryDO> list = componentRepositoryService.getComponentRepositoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组件库.xls", "数据", ComponentRepositoryRespVO.class,
                        BeanUtils.toBean(list, ComponentRepositoryRespVO.class));
    }

}